package com.app.controller.backstage;

import com.app.common.TrainStateTool;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.DateUtil;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.TrainVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Controller
public class TrainController {
    @Autowired
    AdminUserService adminUserService;
    @Autowired
    SkillTrainingService skillTrainingService;
    @Autowired
    TrainEnrollService trainEnrollService;

    @Autowired
    TrainTypeConfigService trainTypeConfigService;

    @Autowired
    private TrainTypeConfigSonService trainTypeConfigSonService;
    @Autowired
    private ConfigService configService;
    @GetMapping("/getTrainList")
    public Result<Object> getTrainList(
            int page, int limit,
            @RequestParam(required = false) String keyWord,
            @RequestParam(required = false) Integer state,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        SkillTrainingModel model = new SkillTrainingModel();
        model.setPage(page);
        model.setLimit(limit);
        model.setKeyWord(keyWord);
        model.setStartTime(startTime);
        model.setEndTime(endTime);
        model.setState(state);
        return Result.success(skillTrainingService.trainList(model));
    }

    @GetMapping("/getTrainStateList")
    public String getTrainStateList() {
        JSONObject json = new JSONObject();
        json.put("data", TrainStateTool.TRAIN_STATE_VO_LIST);
        json.put("code", 200);
        json.put("message", "成功");
        return json.toString();
    }

    @PostMapping("/editTrain")
    public Result<Object> insertTrain(
            @RequestBody SkillTrainingModel model,
            HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        model.setState(1);
        if (model.getId() == 0) {
            skillTrainingService.insert(model);
        } else {
            skillTrainingService.update(model);
        }
        return Result.success("操作成功");
    }

    @PostMapping("/editTrainState")
    public Result<Object> editTrainState(
            int id, int state,
            HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        SkillTrainingModel model = skillTrainingService.getSkillTrainingById(id);
        model.setState(state);
        skillTrainingService.update(model);
        return Result.success("操作成功");
    }

    @GetMapping("/getTrainById")
    public Result<Object> getTrainById(
            int id, HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        SkillTrainingModel model = skillTrainingService.getSkillTrainingById(id);
        List<TrainTypeConfigModel> typeList = trainTypeConfigService.getListById(model.getId());
        model.setTypeConfigModelList(typeList);
        return Result.success(model);
    }

    /**
     * 获取培训班报名列表
     *
     * @param sid
     * @param request
     * @return
     */

    @GetMapping("/getTrainCustomerList")
    public Result<Object> getTrainCustomerList(
            Integer sid,Integer page,Integer limit, HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        TrainEnrollModel model = new TrainEnrollModel();
        model.setSid(sid);
        model.setLimit(limit);
        model.setPage(page);
        Page<TrainEnrollModel> modelPage = trainEnrollService.getModelList(model);
        TrainVo vo = new TrainVo();
        vo.setCount((int)modelPage.getTotal());
        vo.setList(modelPage.getRecords());
        for (int i = 0; i < vo.getList().size(); i++) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Timestamp data = Timestamp.valueOf(vo.getList().get(i).getCrtTime());
            String currentSimpleDateFormat = simpleDateFormat.format(data);
            vo.getList().get(i).setCrtTime(currentSimpleDateFormat);
        }
        return Result.success(vo);
    }

    /**
     * 查询单个专业
     *
     * @param id
     * @param request
     * @return
     */

    @GetMapping("/getSpecialityById")
    public Result<Object> getSpecialityById(
            Integer id,HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        TrainTypeConfigSonModel model = trainTypeConfigSonService.getModelById(id);
        return Result.success(model);
    }

    /**
     * 删除专业
     *
     * @param id
     * @param request
     * @return
     */

    @GetMapping("/delSpecialityById")
    public Result<Object> delSpecialityById(
            Integer id,HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        trainTypeConfigSonService.delete(id);
        return Result.success("操作成功");
    }
    //查询培训专业列表
    @GetMapping("/getTrainSpecialityList")
    public Result<Object> getTrainSpecialityList(HttpServletRequest request,Integer trainId){

        SkillTrainingModel model = skillTrainingService.getSkillTrainingById(trainId);
        List<TrainTypeConfigSonModel> modelList = trainTypeConfigSonService.getListByTrainId(trainId);

        for (int i = 0; i < modelList.size(); i++) {
            //获取一级分类名称
            ConfigModel configModel  = configService.getModelById(modelList.get(i).getTypeId());
            //获取专业名称
            ConfigModel configModel2 = configService.getModelById(modelList.get(i).getConfigId());
            modelList.get(i).setTrainIdText(model.getCompany());
            modelList.get(i).setConfigIdText(configModel2.getName());
            modelList.get(i).setTypeIdText(configModel.getName());
            modelList.get(i).setContentText(configModel2.getName()+"("+configModel.getName()+")");
        }
        return Result.success(modelList);
    }

    /**
     * 编辑专业
     */
    @PostMapping("/editSpeciality")
    @ResponseBody
    public Result editLivePortend(HttpServletRequest request, @RequestBody TrainTypeConfigSonModel model) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        if(model.getId()!=null&&model.getId()>0)
        {
            trainTypeConfigSonService.update(model);
        }else {
            trainTypeConfigSonService.insert(model);
        }
        TrainTypeConfigModel trainTypeConfigModel = new TrainTypeConfigModel();
        trainTypeConfigModel.setTrainId(model.getTrainId());
        trainTypeConfigModel.setType(model.getTypeId());
        List<TrainTypeConfigModel> list = trainTypeConfigService.getListByTrainId(model.getTrainId(),model.getTypeId());
        if(list==null||list.size()==0)
        {
            trainTypeConfigService.insert(trainTypeConfigModel);
        }

        return Result.success("操作成功");
    }

}
